/*
 *@(#)SubjectController.java 1.0 Feb 18, 2008
 *Copyright Fution Bai
 *
 */
package codebush.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import codebush.domain.Article;
import codebush.domain.Comment;
import codebush.domain.TagDataObject;
import codebush.domain.Users;
import codebush.service.Get;
import codebush.service.Subject;
import codebush.service.Tag;
import codebush.util.HttpUtil;

/**
 * 
 * @author Fution Bai
 * @since 1.0
 */
@Controller
public class SubjectController {

	private final Subject subject;
	private final Tag tag;
	private final Get get;

	@Autowired
	public SubjectController(Subject subject, Tag tag, Get get) {
		this.subject = subject;
		this.tag = tag;
		this.get = get;
	}

	public Subject getSubject() {
		return subject;
	}

	public Tag getTag() {
		return tag;
	}

	public Get getGet() {
		return get;
	}

	/**
	 * show an article (article,)by id
	 * 
	 * @param articleId
	 * @param userId
	 * @return
	 */
	@RequestMapping("/thread")
	public ModelAndView showArticle(@RequestParam(value = "aid", required = false)
	String articleId, @RequestParam(value = "uid", required = false)
	String userId) {
		ModelAndView mav = new ModelAndView();
		Map<String, Object> articles = new HashMap<String, Object>();
		if (articleId != null) {
			long aid = Long.parseLong(articleId);
			Article article = this.getGet().getArticle(Long.parseLong(articleId));
			List<Comment> comments = this.getGet().getCommentsByAId(Long.parseLong(articleId));
			List<TagDataObject> articleTags = this.getTag().getTagsByArticleId(aid);
			articles.put("articleTags", articleTags);
			articles.put("article", article);
			articles.put("comments", comments);
			mav.addAllObjects(articles);
		}
		mav.setViewName("/article");
		return mav;
	}

	/**
	 * show an article by the userfriendly link
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/thread/*")
	public ModelAndView showArticle(HttpServletRequest request) {
		// 46 is the length of "/codebush/subject/thread/"
		String linkAlias = request.getRequestURI().substring(25);
		Integer aid = this.getGet().getArticleIdByLinkAlias(linkAlias);
		return showArticle(aid.toString(), null);
	}

	/**
	 * user post a comment to a article, only logged in users available
	 * 
	 * @param articleId
	 * @param comment
	 * @param httpSession
	 * @return
	 */
	@RequestMapping("/reply")
	public ModelAndView reply(@RequestParam(value = "aid", required = true)
	String articleId, @RequestParam(value = "comment", required = false)
	String comment, HttpSession session) {
		ModelAndView mav = new ModelAndView();
		if (comment == null || comment.equals("")) {
			mav.setViewName("redirect:/subject/thread?aid=" + articleId);
			return mav;
		}
		this.getSubject().reply(Long.parseLong(articleId), comment, HttpUtil.getUserId(session));
		mav.setViewName("redirect:/subject/thread?aid=" + articleId);
		return mav;
	}

	/**
	 * show a user's comments
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping("/comments")
	public ModelAndView showComments(@RequestParam(value = "uid", required = true)
	int userId) {
		ModelAndView mav = new ModelAndView();
		Map<String, Object> model = new HashMap<String, Object>();
		List<Comment> comments = this.getGet().getCommentsByUserId(userId);
		Users user = this.getGet().getUser(userId);
		model.put("comments", comments);
		model.put("user", user);
		mav.addAllObjects(model);
		mav.setViewName("/comments");
		return mav;
	}

	/**
	 * delete a comment by the author of the article commented
	 * 
	 * @param commentId
	 * @param articleId
	 * @return
	 */
	@RequestMapping("/deleteComment")
	public ModelAndView deleteComment(@RequestParam(value = "cid", required = true)
	long commentId, @RequestParam(value = "aid", required = true)
	long articleId) {
		ModelAndView mav = new ModelAndView();
		this.getSubject().deleteComment(commentId);
		mav.setViewName("redirect:/subject/thread?aid=" + articleId);
		return mav;
	}

	/**
	 * add a userfriendly link to the article
	 * 
	 * @param linkAlias
	 * @param aid
	 * @return
	 */
	@RequestMapping("/threadAdd:LinkAlias")
	public ModelAndView addLinkAlias(@RequestParam(value = "linkAlias", required = false)
	String linkAlias, @RequestParam(value = "aid", required = false)
	String articleId) {
		ModelAndView mav = new ModelAndView();
		if(linkAlias == null){
			mav.setViewName("/support/add_link_alias");
			return mav;
		}
		if(linkAlias != null && articleId != null){
			this.getSubject().addLinkAliasToAnArticle(Integer.parseInt(articleId), linkAlias);
		}
		mav.setViewName("redirect:/");
		return mav;
	}
}
